package utils;

import static play.libs.Json.toJson;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.util.Duration;
import play.Play;
import play.api.templates.Html;
import play.libs.Akka;
import play.libs.WS;
import play.libs.F.Promise;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.*;
import models.*;
public class MailerScheduler {

   	
	public static void newAppointment(
			AppointmentHistory appointment ,
			User user
            ) {
		User duser = User.getUserDetails(appointment.doctor.id);
		Hospitals hospital =null;
		if(null !=appointment.hospital){
			hospital= Hospitals.getHospital(appointment.hospital.id);
			
		}else{
			hospital= new Hospitals();
			hospital.hospitalName="";
		}
		Html messageBody = newappointment.render(duser.name,user.name,hospital.hospitalName,appointment.appointmentHeadline,appointment.appointmentTime,appointment.appointmentComment);
		//System.out.println(messageBody);
		String[] emails = {duser.email};
		String subject = "New appointment Notification";
		scheduleMail(emails, subject,messageBody.toString());
	}
	
	public static void referredPatient(
			AppointmentHistory appointment ,
			User user, boolean newUser
            ) {
		User duser = User.getUserDetails(appointment.doctor.id);
		User puser = User.getUserDetails(appointment.patient.id);
		Hospitals hospital =null;

		Html messageBody = referredpatient.render(duser,puser,user,appointment, newUser);
		//System.out.println(messageBody);
		String[] emails = {duser.email};
		String subject = "Referral Notification";
		scheduleMail(emails, subject,messageBody.toString());
	}


	public static void scheduleMail(String[] emails, String subject, String body){
      	 ActorRef testActor = Akka.system().actorOf(new Props(MailSender.class));
      	 MessageCarrier m = new MessageCarrier();
      	 	m.emails =emails;
      	 	m.subject=subject;
      	 	m.body = body;
      	     Akka.system().scheduler().scheduleOnce(
      	          Duration.create(1, TimeUnit.SECONDS),
      	          testActor, 
      	          m
      	      );

	}

	  public static void sendGCMNotification(AppointmentHistory appointment ,
				User user,String message){
		  System.out.println("calling actor");
	    	 ActorRef testActor = Akka.system().actorOf(new Props(GCMSender.class));
	      	 GCMCarrier m = new GCMCarrier();
	      	 	m.appointment =appointment;	      	 	
	      	 	m.user=user;
	      	 	m.message = message;
	      	     Akka.system().scheduler().scheduleOnce(
	      	          Duration.create(1, TimeUnit.SECONDS),
	      	          testActor, 
	      	          m
	      	      );
	  }
	
}